﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data;

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class Ann_Result
{
    [DataMember(Order = 0)]
    public string id { get; set; }
    [DataMember(Order = 1)]
    public string title { get; set; }
    [DataMember(Order = 2)]
    public string content { get; set; }
    [DataMember(Order = 3)]
    public string ins_datetime { get; set; }
    [DataMember(Order = 4)]
    public string upd_datetime { get; set; }
}

public partial class Service_Ann : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MyDb.Db db = new MyDb.Db())
        {
            IList<Ann_Result> list = new List<Ann_Result>();
            Ann_Result item = null;
            string sql = "";
            if (!string.IsNullOrEmpty(Request.QueryString["annid"]))
                sql=string.Format("select * from Announcement where ID='{0}' order by InsDT desc", Request.QueryString["annid"]);
            else
                sql="select * from Announcement order by InsDT desc";
            using (DataTable dt2 = db.getDataTable(sql, "mess2"))
            {
                if (!string.IsNullOrEmpty(Request.QueryString["annid"]))
                {
                    if (dt2.Rows.Count >= 1)
                    {
                        DataRow rs = dt2.Rows[0];
                        item = new Ann_Result();
                        item.title = rs["Subject"].ToString();
                        item.id = rs["ID"].ToString();
                        item.content = rs["Content"].ToString();
                        item.ins_datetime = rs["InsDT"].ToString();
                        item.upd_datetime = rs["UpdDT"].ToString();
                        list.Add(item);
                    }
                }
                else
                {
                    foreach (DataRow rs in dt2.Rows)
                    {
                        item = new Ann_Result();
                        item.title = rs["Subject"].ToString();
                        item.id = rs["ID"].ToString();
                        item.content = "";
                        item.ins_datetime = rs["InsDT"].ToString();
                        item.upd_datetime = rs["UpdDT"].ToString();
                        list.Add(item);
                    }
                }
            }
            Response.Write(string.Format("{{\"announcement\":{0}}}", MyDb.JSON.stringify(list)));
            list.Clear();
            list = null;
        }
        Response.End();
    }
}
